﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using JEdwonTune.Lib.Crud;
using JEdwonTune.Lib;
using JEdwonTune.Common;

namespace JEdwonTune.Crud {
    public partial class FrmEditProperty : FrmXmlNodeEditorBase {
        public FrmEditProperty() {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e) {
            if (String.IsNullOrEmpty(this.txtName.Text.Trim())) {
                MessageBox.Show("请输入Name");
                return;
            }
            base.SetNodeAttrValue(base.Node,"name", this.txtName.Text.Trim());
            base.SetNodeAttrValue(base.Node,"order", this.cbxOrder.SelectedValue.ToString());
            base.SetNodeAttrValue(base.Node,"data-type", this.cbxDataType.SelectedValue.ToString());
            base.SetNodeAttrValue(base.Node,"in-list", (this.cbInlist.Checked).ToString().ToLower());
            base.SetNodeAttrValue(base.Node,"in-form", (this.cbInform.Checked).ToString().ToLower());
            base.SetNodeAttrValue(base.Node,"in-search", (this.cbInSearch.Checked).ToString().ToLower());
            base.SetNodeAttrValue(base.Node,"editable", (this.cbEditable.Checked).ToString().ToLower());
            base.SetNodeAttrValue(base.Node, "datasource", this.cbxDataSource.SelectedValue.ToString());
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void BindDataSourceList() {
            XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable());
            xnm.AddNamespace("ns", Doc.DocumentElement.NamespaceURI);
            XmlNodeList nl = base.Doc.SelectNodes("//ns:datasource/ns:resource",xnm);
            IList<KeyValuePair<String, String>> list = new List<KeyValuePair<String, String>>();
            list.Add(new KeyValuePair<string, string>("", ""));
            foreach (XmlNode n in nl) {
                String _key = n.Attributes["name"].Value;
                String _value = n.Attributes["name"].Value;
                list.Add(new KeyValuePair<string, string>(_key,_value));
            }
            this.cbxDataSource.DisplayMember = "Key";
            this.cbxDataSource.ValueMember = "Value";
            this.cbxDataSource.DataSource = list;
            if (base.Node.Attributes["datasource"] != null)
                this.cbxDataSource.SelectedValue = base.Node.Attributes["datasource"].Value;
        }
        private void FrmEditProperty_Load(object sender, EventArgs e) {
            if (base.Node == null) {
                return;
            }
            if (base.Node.Name != "property") {
                MessageBox.Show("编辑的节点不是property节点");
                this.Close();
                return;
            }
            if (base.Node.Attributes["name"] != null) {
                this.txtName.Text = base.Node.Attributes["name"].Value;
            }
            this.cbxDataType.ValueMember = "Key";
            this.cbxDataType.DisplayMember = "Value";
            this.cbxDataType.DataSource = Ctrls.GetDataTypeItems();
            if (base.Node.Attributes["data-type"] != null) {
                this.cbxDataType.SelectedValue = base.Node.Attributes["data-type"].Value;
            }
            this.cbxOrder.ValueMember = "Key";
            this.cbxOrder.DisplayMember = "Value";
            this.cbxOrder.DataSource = Ctrls.GetPropertyOrderItems();
            if (base.Node.Attributes["order"] != null) {
                this.cbxOrder.SelectedValue = base.Node.Attributes["order"].Value;
            }

            BindDataSourceList();

            this.cbInlist.Checked = XmlHelper.ReadBooleanAttr(base.Node, "in-list", true);
            this.cbInform.Checked = XmlHelper.ReadBooleanAttr(base.Node, "in-form", true);
            this.cbInSearch.Checked = XmlHelper.ReadBooleanAttr(base.Node, "in-search", false);
            this.cbEditable.Checked = XmlHelper.ReadBooleanAttr(base.Node, "editable", true);

        }
    }
}
